﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using System.Xml.Linq;
using MindBreakerResources;
using Microsoft.Xna.Framework.Graphics;

namespace MindBreaker.LevelManagement
{
    class Level : DrawableGameComponent
    {
        /***************/
        /** CONSTANTS **/
        /***************/

        private const int DEFAULT_COLUMN_SIZE = 16;
        private const int DEFAULT_ROW_SIZE = 20;

        /****************/
        /** ATTRIBUTES **/
        /****************/

        private int _rowSize;
        private int _columnSize;
        private Dictionary<int, LevelCase> mapCases;
        private Texture2D background;
        private SpriteBatch spriteBatch;

        /******************/
        /** CONSTRUCTORS **/
        /******************/

        public Level(Game game, int rowSize, int columnSize) : base(game)
        {
            this.Init(game, rowSize, columnSize);
        }

        public Level(Game game) : base(game)
        {
            this.Init(game, DEFAULT_COLUMN_SIZE, DEFAULT_ROW_SIZE);
        }

        public Level(Game game, String levelFile) : base(game)
        {
            this.Init(game, DEFAULT_COLUMN_SIZE, DEFAULT_ROW_SIZE);
            // Load xml datas
            List<LevelCaseXml> caseList = game.Content.Load<List<LevelCaseXml>>(levelFile);
            Console.WriteLine("Chargement de " + caseList.Count + " case depuis le level '" + levelFile + "'");
            // Create cases
            int index;
            foreach(LevelCaseXml currentCase in caseList) {
                index = currentCase.posX + currentCase.posY * DEFAULT_COLUMN_SIZE;
                this.mapCases.Add(index, new LevelCase(currentCase.posX, currentCase.posY, game, currentCase.spriteKey));
            }
            this.DrawOrder = 1;
            game.Components.Add(this);
        }

        private void Init(Game game, int columnSize, int rowSize)
        {
            // Initialisation des variables de niveau
            this.RowSize = rowSize;
            this.ColumnSize = columnSize;
            this.mapCases = new Dictionary<int, LevelCase>(rowSize * columnSize);
        }


        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Chargement des données pour le background
            this.spriteBatch = new SpriteBatch(GraphicsDevice);
            this.background = TextureManager.getInstance(this.Game).getTexture(@"images\gameBackground");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            // Affichage du fond
            this.spriteBatch.Begin();
            this.spriteBatch.Draw(this.background, new Rectangle(0, 0, this.background.Width, this.background.Height), Color.White);
            this.spriteBatch.End();
            base.Draw(gameTime);
        }


        /******************/
        /** PROPRIETE C# **/
        /******************/

        public int ColumnSize
        {
            get { return _columnSize; }
            set { _columnSize = value; }
        }

        public int RowSize
        {
            get { return _rowSize; }
            set { _rowSize = value; }
        }
    }
}
